package com.example.su_shi_tan_mine.child

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSON
import com.example.su_shi_tan_bas_lib.adapter.multi22.JssBaseMultiAdapter
import com.example.su_shi_tan_bas_lib.base.BaseFragmentEx
import com.example.su_shi_tan_bas_lib.base.LazyFragment
import com.example.su_shi_tan_bas_lib.base.MultiListFragment
import com.example.su_shi_tan_bas_lib.bean.CommonBean
import com.example.su_shi_tan_bas_lib.networking.RestClient
import com.example.su_shi_tan_mine.R
import com.example.su_shi_tan_mine.child.bean.MessagePageBean
import com.example.su_shi_tan_mine.child.bean.ServiceCustomerListBean
import com.example.su_shi_tan_mine.child.provider.ServiceCustomerListReceiverProvider
import com.example.su_shi_tan_mine.child.provider.ServiceCustomerListSendingProvider
import com.example.su_shi_tan_mine.child.provider.ServiceCustomerListUnknownProvider
import com.example.su_shi_tan_mine.child.provider.UnfilledHeight
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class ServiceCustomerListFragment(mUnfilledHeight: UnfilledHeight) :
    MultiListFragment<ServiceCustomerListBean>(), UnfilledHeight by mUnfilledHeight {

    companion object {
        fun newInstance(mUnfilledHeight: UnfilledHeight): ServiceCustomerListFragment {
            val args = Bundle()
            val fragment = ServiceCustomerListFragment(mUnfilledHeight)
            fragment.arguments = args
            return fragment
        }
    }

    private var lastPageIndex = pageIndex

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        isAddDividerItemDecoration = false
        isEnableLoadMore = false
    }

    override fun initView(view: View) {
        super.initView(view)
        mRecyclerView?.overScrollMode = View.OVER_SCROLL_NEVER
        mRecyclerView?.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val layoutManager = recyclerView.layoutManager
                if (layoutManager is LinearLayoutManager) {
                    val childCount = recyclerView.childCount
                    if (childCount > 0) {
                        val lastChildView = recyclerView.getChildAt(childCount - 1)
                        val bottom = lastChildView.bottom
                        val listHeight: Int =
                            recyclerView.height - recyclerView.paddingBottom
                        setUnfilledHeight(listHeight - bottom)
                    }
                }
            }
        })
    }

    override fun onRefresh() {
        if (pageIndex == lastPageIndex) {
            super.onRefresh()
            lastPageIndex++
        } else {
            pageIndex++
            mSwipeRefreshLayout?.postDelayed({ netRequest() }, delayedTime.toLong())
        }

    }

    override fun loadListDate(collection: Collection<ServiceCustomerListBean?>?) {
        if (collection == null || collection.isEmpty()) {
            if (pageIndex == 1) {
                onFailed()
            } else {
                if (mSwipeRefreshLayout?.isRefreshing==true){
                    mSwipeRefreshLayout?.isRefreshing=false
                }
                mAdapter?.loadMoreModule?.loadMoreEnd(false)
            }
            return
        }
        if (pageIndex == 1) {
            mAdapter?.replaceData(collection)
            val loadMoreModule = mAdapter?.loadMoreModule
            loadMoreModule?.isEnableLoadMoreIfNotFullPage = true

        } else {
            mAdapter?.addData(0, collection)
        }
        mAdapter?.loadMoreModule?.loadMoreComplete()
        if (mSwipeRefreshLayout?.isRefreshing == true) {
            mSwipeRefreshLayout?.isRefreshing = false
        }
        mSwipeRefreshLayout?.isEnabled = isAlwaysRefresh
        mAdapter?.loadMoreModule?.checkDisableLoadMoreIfNotFullPage()
    }

    override fun addOnListItemProvider(mAdapter: JssBaseMultiAdapter<ServiceCustomerListBean?>?) {
        mAdapter?.addItemProvider(ServiceCustomerListReceiverProvider())
        mAdapter?.addItemProvider(ServiceCustomerListSendingProvider())
        mAdapter?.addItemProvider(ServiceCustomerListUnknownProvider())
    }

    override fun netRequest() {
        RestClient.builder()
            .url(R.string.converse_lst)
            .params("page", pageIndex)
            .params("limit", 10)
            .success {
                parseDate(it)
            }
            .failure {
                onFailed()
            }.error { _, _ ->
                onFailed()
            }.build().get()
    }


    override fun preEmptyView() {
        super.preEmptyView()
        val textView: TextView? = list_empty_view?.findViewById(R.id.textView)
        textView?.visibility = View.GONE
    }


    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val mArray = JSON.parseArray(data, ServiceCustomerListBean::class.java)
        loadListDate(mArray)
        val size = mAdapter?.data?.size ?: 1
        mRecyclerView?.scrollToPosition(size - 1)
        delayedTime = 0
    }


    override fun refreshData() {
//        super.refreshData()
        pageIndex = 1
        lastPageIndex = pageIndex
        if (mSwipeRefreshLayout?.isRefreshing == false) {
            mSwipeRefreshLayout?.isRefreshing = true
        }
        onRefresh()
    }


    override val listType: Type = TypeBuilder.newInstance(CommonBean::class.java)
        .beginSubType(List::class.java)
        .addTypeParam(ServiceCustomerListBean::class.java).endSubType().build()

}